package com.xyd.service.impl;

import com.xyd.entity.Cart;
import com.xyd.mapper.CartMapper;
import com.xyd.service.CartService;
import com.xyd.util.MybatisUtil;
import java.util.List;
import org.apache.ibatis.session.SqlSession;

/**
 * 类描述：
 *
 * @author 张吉哲
 * @date 2021/1/14 18:47
 */
public class CartServiceImpl implements CartService {

  private SqlSession sqlSession;

  @Override
  public Boolean addProduct(Long epId, String euUserId) {
    sqlSession = MybatisUtil.getSqlSession();
    boolean flag = sqlSession.getMapper(CartMapper.class).addProduct(epId, euUserId);
    if (flag) {
      sqlSession.commit();
    }
    MybatisUtil.closeSqlSession(sqlSession);
    return flag;
  }

  @Override
  public boolean updateOneProduct(Long epId, String euUserId) {
    sqlSession = MybatisUtil.getSqlSession();
    boolean flag = sqlSession.getMapper(CartMapper.class).updateOneProduct(epId, euUserId);
    if (flag) {
      sqlSession.commit();
    }
    MybatisUtil.closeSqlSession(sqlSession);
    return flag;
  }

  @Override
  public boolean changeNum(String euUserId, String epId, String newNum) {
    sqlSession = MybatisUtil.getSqlSession();
    boolean flag = sqlSession.getMapper(CartMapper.class).changeNum(euUserId, epId, newNum);
    if (flag) {
      sqlSession.commit();
    }
    MybatisUtil.closeSqlSession(sqlSession);
    return flag;
  }

  @Override
  public List<Cart> findAllMyCart(String euUserId) {
    sqlSession = MybatisUtil.getSqlSession();
    List<Cart> carts = sqlSession.getMapper(CartMapper.class).findAllMyCart(euUserId);
    MybatisUtil.closeSqlSession(sqlSession);
    return carts;
  }
}
